package config

import (
	"gitee.com/gousing/values"
)

// GetSliceAny 读取配置项并转换为 []any 切片, 如配置项不存在或转换失败, 则返回空的切片
func GetSliceAny(key string) []any {
	return GetSliceAnyD(key, []any{})
}

// GetSliceAnyD 读取配置项并转换为 []any 切片, 如配置项不存在或转换失败, 则返回defaultVal指定的切片
func GetSliceAnyD(key string, defaultVal []any) []any {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToSliceAnyE(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}

// GetSliceAnyE 读取配置项并转换为 []any 切片, 如配置项不存在或转换失败,则返回空的切片和错误信息
func GetSliceAnyE(key string) ([]any, error) {
	if v, ok := GetExist(key); !ok {
		return []any{}, ErrKeyNotExist
	} else {
		return values.ToSliceAnyE(v)
	}
}

// GetSliceAs 读取配置项并转换为 []TypeBase 或 [][]TypeBase 切片, 如配置项不存在或转换失败, 则返回空的切片
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration| time.Month | time.Weekday
func GetSliceAs[T values.TypeBase | values.TypeBaseSlice](key string) []T {
	return GetSliceAsD(key, []T{})
}

// GetSliceAsD 读取配置项并转换为 []TypeBase 或 [][]TypeBase 切片, 如配置项不存在或转换失败, 则返回defaultVal指定的切片
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration| time.Month | time.Weekday
func GetSliceAsD[T values.TypeBase | values.TypeBaseSlice](key string, defaultVal []T) []T {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToSliceAsE[T](v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}

// GetSliceAsE 读取配置项并转换为 []TypeBase 或 [][]TypeBase 切片, 如配置项不存在或转换失败, 则返回空的切片和错误信息
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration| time.Month | time.Weekday
func GetSliceAsE[T values.TypeBase | values.TypeBaseSlice](key string) ([]T, error) {
	if v, ok := GetExist(key); !ok {
		return []T{}, ErrKeyNotExist
	} else {
		return values.ToSliceAsE[T](v)
	}
}

// GetSliceMap 读取配置项并转换为 []map[K]V 切片, 如配置项不存在或转换失败, 则返回空的切片
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration| time.Month | time.Weekday
func GetSliceMap[K, V values.TypeBase](key string) []map[K]V {
	return GetSliceMapD(key, []map[K]V{})
}

// GetSliceMapD 读取配置项并转换为 []map[K]V 切片, 如配置项不存在或转换失败, 则返回defaultVal指定的切片
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration| time.Month | time.Weekday
func GetSliceMapD[K, V values.TypeBase](key string, defaultVal []map[K]V) []map[K]V {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToSliceMapE[K, V](v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}

// GetSliceMapE 读取配置项并转换为 []map[K]V 切片, 如配置项不存在或转换失败, 则返回空的切片和错误信息
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration | time.Month | time.Weekday
//   - 转换时切片中的map元素如为nil将会忽略(跳过)
func GetSliceMapE[K, V values.TypeBase](key string) ([]map[K]V, error) {
	if v, ok := GetExist(key); !ok {
		return []map[K]V{}, ErrKeyNotExist
	} else {
		return values.ToSliceMapE[K, V](v)
	}
}
